﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using RealState.BLL;
using log4net;
using System.Collections.Generic;
using RealState.Utils;
using RealState.BusinessObjects;

namespace RealState.W.AdminControl
{
    public partial class ContentList : System.Web.UI.UserControl
    {
        private ContentsBLL m_ContentsBLL;
        private Contents m_ContentDO;
        DataTable dt;
        protected ILog log = LogManager.GetLogger(typeof(ContentList));
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack) {
                m_ContentsBLL = new ContentsBLL();
                try
                {
                    //GridView1.DataSource = m_ContentsBLL.GetContentsALL();
                    dt = Converter.ToDataTable(m_ContentsBLL.GetContentsALL());
                    //GridView1.DataBind();
                    rptCategories.DataSource = m_ContentsBLL.GetContentsALL();
                    rptCategories.DataBind();
                }
                catch (Exception ex)
                {
                    log.Error(ex.Message);

                }
                finally {
                    m_ContentsBLL = null;  
                }
            }
        }

        protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName.Equals("detail"))
            {
                m_ContentsBLL = new ContentsBLL();
                try
                {
                    int index = Convert.ToInt32(e.CommandArgument);
                    int code = Int32.Parse(GridView1.DataKeys[index].Value.ToString());

                    //IEnumerable<DataRow> query = from i in dt.AsEnumerable()
                    //                             where i.Field<Int32>("Id").Equals(code)
                    //                             select i;
                    DataTable detailTable = Converter.ToDataTable(m_ContentsBLL.GetContentsData(code));
                    DetailsView1.DataSource = detailTable;
                    DetailsView1.DataBind();
                    System.Text.StringBuilder sb = new System.Text.StringBuilder();
                    sb.Append("<script type='text/javascript'>");
                    sb.AppendLine("$(document).ready(function() {");
                    sb.AppendLine("$('#currentdetail').modal('show');});");
                    sb.Append("</script>");
                    //ScriptManager.RegisterStartupScript(Page, GetType(), "MyScript", "alert('message here');", true);
                    ScriptManager.RegisterClientScriptBlock(Page, GetType(), "ModalScript", sb.ToString(), false);

                    //ScriptManager.RegisterClientScriptBlock(this, this.GetType(),
                    //           "ModalScript", sb.ToString(), false);
                }
                catch (Exception ex)
                {
                    log.Error(ex.Message);

                }
                finally
                {
                    m_ContentsBLL = null;
                }
                

            }
        }

        protected string GetStatus(object obj)
        {
            if (Convert.ToBoolean(obj)==true)
                return "Hiển thị";
            return "Ẩn";
        }

        protected string GetSpecial(object obj)
        {
            if (Convert.ToBoolean(obj)==true)
                return "VIP";
            return "Thường";
        }
        protected string GetDatetime(object obj)
        {
            return Convert.ToDateTime(obj).ToString("dd/MM/yyyy");
        }

        protected void rptCategories_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName.Equals("status"))
            {
                HiddenField hdnSec = e.Item.FindControl("hdnSecID") as HiddenField;
                HiddenField hdnSstatus = e.Item.FindControl("hdStatus") as HiddenField;
                m_ContentsBLL = new ContentsBLL();
                m_ContentDO = new Contents();
                try
                {
                    m_ContentDO = m_ContentsBLL.GetContents(Int32.Parse(hdnSec.Value));
                    if (Convert.ToBoolean(hdnSstatus.Value))
                        m_ContentDO.IsActive = false;
                    else
                        m_ContentDO.IsActive = true;                
                    int result = m_ContentsBLL.UpdateContents(m_ContentDO);
                    if (result > 0)
                    {
                        JavaScript.Success("Cập nhật thành công", Page, "Content.aspx");
                    }
                    else
                        JavaScript.Error("Có lỗi xảy ra ", Page);
                }
                catch (Exception ex)
                {
                    log.Error(ex.Message);

                }
                finally
                {
                    m_ContentsBLL = null;
                    m_ContentDO = null;
                }
                
            }
            if (e.CommandName.Equals("detail"))
            {
                m_ContentsBLL = new ContentsBLL();
                try
                {

                    HiddenField hdnSec = e.Item.FindControl("hdnSecID") as HiddenField;
                    int code = Int32.Parse(hdnSec.Value);
                    DataTable detailTable = Converter.ToDataTable(m_ContentsBLL.GetContentsData(code));
                    DetailsView1.DataSource = detailTable;
                    DetailsView1.DataBind();
                    System.Text.StringBuilder sb = new System.Text.StringBuilder();
                    sb.Append("<script type='text/javascript'>");
                    sb.AppendLine("$(document).ready(function() {");
                    sb.AppendLine("$('#currentdetail').modal('show');});");
                    sb.Append("</script>");
                    //ScriptManager.RegisterStartupScript(Page, GetType(), "MyScript", "alert('message here');", true);
                    ScriptManager.RegisterClientScriptBlock(Page, GetType(), "ModalScript", sb.ToString(), false);

                    //ScriptManager.RegisterClientScriptBlock(this, this.GetType(),
                    //           "ModalScript", sb.ToString(), false);
                }
                catch (Exception ex)
                {
                    log.Error(ex.Message);

                }
                finally
                {
                    m_ContentsBLL = null;
                }
            }
        }

        protected void rptCategories_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType.Equals(ListItemType.AlternatingItem) || e.Item.ItemType.Equals(ListItemType.Item))
            {
                Button cbox = e.Item.FindControl("Button2") as Button;
                HiddenField hdnSstatus = e.Item.FindControl("hdStatus") as HiddenField;
                if (Convert.ToBoolean(hdnSstatus.Value))
                {
                    cbox.Text = "Ẩn";
                }
                else {
                    cbox.Text = "Hiện";
                }
            }
        }



    }
}